package com.qpkj.web.controller.oms;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.qpkj.common.constant.UserConstants;
import com.qpkj.common.utils.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.qpkj.common.annotation.Log;
import com.qpkj.common.core.controller.BaseController;
import com.qpkj.common.core.domain.AjaxResult;
import com.qpkj.common.enums.BusinessType;
import com.qpkj.oms.domain.OmsProductCategory;
import com.qpkj.oms.service.IOmsProductCategoryService;
import com.qpkj.common.utils.poi.ExcelUtil;
import com.qpkj.common.core.page.TableDataInfo;

/**
 * 物料分类Controller
 *
 * @author ruoyi
 * @date 2022-12-20
 */
@RestController
@RequestMapping("/oms/category")
public class OmsProductCategoryController extends BaseController {
    @Autowired
    private IOmsProductCategoryService omsProductCategoryService;

    /**
     * 查询物料分类列表
     */
    @PreAuthorize("@ss.hasPermi('oms:category:list')")
    @GetMapping("/list")
    public TableDataInfo list(OmsProductCategory omsProductCategory) {
        startPage();
        List<OmsProductCategory> list = omsProductCategoryService.selectOmsProductCategoryList(omsProductCategory);
        return getDataTable(list);
    }

    /**
     * 导出物料分类列表
     */
    @PreAuthorize("@ss.hasPermi('oms:category:export')")
    @Log(title = "物料分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OmsProductCategory omsProductCategory) {
        List<OmsProductCategory> list = omsProductCategoryService.selectOmsProductCategoryList(omsProductCategory);
        ExcelUtil<OmsProductCategory> util = new ExcelUtil<OmsProductCategory>(OmsProductCategory.class);
        util.exportExcel(response, list, "物料分类数据");
    }

    /**
     * 获取物料分类详细信息
     */
    @PreAuthorize("@ss.hasPermi('oms:category:query')")
    @GetMapping(value = "/{categoryId}")
    public AjaxResult getInfo(@PathVariable("categoryId") Long categoryId) {
        return success(omsProductCategoryService.selectOmsProductCategoryByCategoryId(categoryId));
    }

    /**
     * 新增物料分类
     */
    @PreAuthorize("@ss.hasPermi('oms:category:add')")
    @Log(title = "物料分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OmsProductCategory omsProductCategory) {
        if (UserConstants.NOT_UNIQUE.equals(omsProductCategoryService.checkNameUnique(omsProductCategory))) {
            return error("新增物料分类'" + omsProductCategory.getCategoryName() + "'失败，分类名称已存在");
        }
        omsProductCategory.setCreateBy(getUsername());
        return toAjax(omsProductCategoryService.insertOmsProductCategory(omsProductCategory));
    }

    /**
     * 修改物料分类
     */
    @PreAuthorize("@ss.hasPermi('oms:category:edit')")
    @Log(title = "物料分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OmsProductCategory omsProductCategory) {
        return toAjax(omsProductCategoryService.updateOmsProductCategory(omsProductCategory));
    }

    /**
     * 删除物料分类
     */
    @PreAuthorize("@ss.hasPermi('oms:category:remove')")
    @Log(title = "物料分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/{categoryIds}")
    public AjaxResult remove(@PathVariable Long[] categoryIds) {
        return toAjax(omsProductCategoryService.deleteOmsProductCategoryByCategoryIds(categoryIds));
    }
}
